﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Slimes
{
    public class Slime : DrawableGameComponent
    {
        public static Texture2D slimeTexture;
        public static Texture2D pixelTexture;
        bool right, down;
        private int _x = 100;
        public int x
        {
            get { return _x; }
            set
            {
                _x = value;
                if (_x + width > 580 || _x < 0) right = !right;
            }
        }
        private int _y = 100;
        public int y
        {
            get
            {
                return _y;
            }
            set
            {
                _y = value;
                if (_y + height > 480 || _y < 0) down = !down;
            }
        }
        int height = 50;
        int width = 50;
        static int count = 0;
        public bool selected { get; set; }


        private Slime(Game game, SlimeColor one, SlimeColor two)
            : base(game)
        {
            this.one = one;
            this.two = two;
            rand = new Random(count++);
            x = rand.Next(580 - width);
            y = rand.Next(480 - height);
            switch (rand.Next(2))
            {
                case 0:
                    right = true;
                    break;
                default:
                    right = false;
                    break;
            }
            switch (rand.Next(2))
            {
                case 0:
                    right = true;
                    break;
                default:
                    right = false;
                    break;
            }
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

            spriteBatch.Begin();
            Rectangle drawAt = new Rectangle(x,y,height,width);
            if (selected)
            {
                spriteBatch.Draw(pixelTexture, drawAt, color);
                spriteBatch.Draw(pixelTexture, new Rectangle(x + 3, y + 3, height - 6, width - 6), Color.Black);
            }

            spriteBatch.Draw(slimeTexture, drawAt, color);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            if (right) { x++; }
            else { x--; }
            if (down) { y++; }
            else { y--; }

            base.Update(gameTime);
        }

        public override string ToString()
        {
            return color + ": " + one + " " + two;
        }

        #region Slime
        static Random rand = new Random();

        public enum SlimeColor { WHITE, PURPLE, BLUE, GREEN, YELLOW, RED }

        private SlimeColor one { get; set; }
        private SlimeColor two { get; set; }
        public Microsoft.Xna.Framework.Color color
        {
            get
            {
                if (one == SlimeColor.RED || two == SlimeColor.RED)
                {
                    return Microsoft.Xna.Framework.Color.Red;
                }
                else if (one == SlimeColor.YELLOW || two == SlimeColor.YELLOW)
                {
                    return Microsoft.Xna.Framework.Color.Yellow;
                }
                else if (one == SlimeColor.GREEN || two == SlimeColor.GREEN)
                {
                    return Microsoft.Xna.Framework.Color.Green;
                }
                else if (one == SlimeColor.BLUE || two == SlimeColor.BLUE)
                {
                    return Microsoft.Xna.Framework.Color.Blue;
                }
                else if (one == SlimeColor.PURPLE || two == SlimeColor.PURPLE)
                {
                    return Microsoft.Xna.Framework.Color.Purple;
                }
                else
                {
                    return Microsoft.Xna.Framework.Color.White;
                }
            }
        }

        public SlimeColor slimeColor
        {
            get
            {
                if (one == SlimeColor.RED || two == SlimeColor.RED)
                {
                    return SlimeColor.RED;
                }
                else if (one == SlimeColor.YELLOW || two == SlimeColor.YELLOW)
                {
                    return SlimeColor.YELLOW;
                }
                else if (one == SlimeColor.GREEN || two == SlimeColor.GREEN)
                {
                    return SlimeColor.GREEN;
                }
                else if (one == SlimeColor.BLUE || two == SlimeColor.BLUE)
                {
                    return SlimeColor.BLUE;
                }
                else if (one == SlimeColor.PURPLE || two == SlimeColor.PURPLE)
                {
                    return SlimeColor.PURPLE;
                }
                else
                {
                    return SlimeColor.WHITE;
                }
            }
        }

        #endregion Slime

        #region Static Methods

        public static void mate(Game game, Slime mom, Slime dad, int numberOfChildren, List<Slime> children)
        {
            SlimeColor tempOne;
            SlimeColor tempTwo;


            for (int x = 0; x < numberOfChildren; x++)
            {
                switch (rand.Next(2))
                {
                    case 1:
                        tempOne = mom.one;
                        break;
                    default:
                        tempOne = mom.two;
                        break;
                }
                switch (rand.Next(2))
                {
                    case 1:
                        tempTwo = dad.one;
                        break;
                    default:
                        tempTwo = dad.two;
                        break;
                }

                children.Add(new Slime(game, tempOne, tempTwo));
            }

        }

        public static Slime newRed(Game game)
        {
            SlimeColor tempOne;
            switch (rand.Next(6))
            {
                case 5:
                    tempOne = SlimeColor.WHITE;
                    break;
                case 4:
                    tempOne = SlimeColor.PURPLE;
                    break;
                case 3:
                    tempOne = SlimeColor.BLUE;
                    break;
                case 2:
                    tempOne = SlimeColor.GREEN;
                    break;
                case 1:
                    tempOne = SlimeColor.YELLOW;
                    break;
                default:
                    tempOne = SlimeColor.RED;
                    break;
            }

            return new Slime(game, SlimeColor.RED, tempOne);
        }

        public static Slime newYellow(Game game)
        {
            SlimeColor tempOne;
            switch (rand.Next(5))
            {
                case 4:
                    tempOne = SlimeColor.WHITE;
                    break;
                case 3:
                    tempOne = SlimeColor.PURPLE;
                    break;
                case 2:
                    tempOne = SlimeColor.BLUE;
                    break;
                case 1:
                    tempOne = SlimeColor.GREEN;
                    break;
                default:
                    tempOne = SlimeColor.YELLOW;
                    break;
            }

            return new Slime(game, SlimeColor.YELLOW, tempOne);
        }

        public static Slime newGreen(Game game)
        {
            SlimeColor tempOne;
            switch (rand.Next(4))
            {
                case 3:
                    tempOne = SlimeColor.WHITE;
                    break;
                case 2:
                    tempOne = SlimeColor.PURPLE;
                    break;
                case 1:
                    tempOne = SlimeColor.BLUE;
                    break;
                default:
                    tempOne = SlimeColor.GREEN;
                    break;
            }

            return new Slime(game, SlimeColor.GREEN, tempOne);
        }

        public static Slime newBlue(Game game)
        {
            SlimeColor tempOne;
            switch (rand.Next(3))
            {
                case 2:
                    tempOne = SlimeColor.WHITE;
                    break;
                case 1:
                    tempOne = SlimeColor.PURPLE;
                    break;
                default:
                    tempOne = SlimeColor.BLUE;
                    break;
            }

            return new Slime(game, SlimeColor.BLUE, tempOne);
        }

        public static Slime newPurple(Game game)
        {
            SlimeColor tempOne;
            switch (rand.Next(3))
            {
                case 1:
                    tempOne = SlimeColor.WHITE;
                    break;
                default:
                    tempOne = SlimeColor.PURPLE;
                    break;
            }

            return new Slime(game, SlimeColor.PURPLE, tempOne);
        }

        public static Slime newWhite(Game game)
        {
            return new Slime(game, SlimeColor.WHITE, SlimeColor.WHITE);
        }

        #endregion statics
    }
}
